package com.glink.manage.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.vehicle.*;
import com.glink.manage.service.BarrierGateService;
import com.glink.manage.service.VehicleAccessRelaService;
import com.glink.manage.service.VehicleService;
import com.glink.manage.vo.vehicle.VehicleAccessPageVO;
import com.glink.manage.vo.vehicle.VehicleLicensePlateVO;
import com.glink.manage.vo.vehicle.VehiclePageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/API/Manage/vehicleManage")
@Api(tags = "车辆和特种设备管理")
@Slf4j
public class VehicleController extends AbstractBaseController{

    @Value("${licensePlateRegex:^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵青藏川宁琼港澳]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4,5}$|^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵青藏川宁琼]{1}[A-Z]{1}[D|F]{1}[A-HJ-NP-Z0-9]{5}$|^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵青藏川宁琼]{1}[A-Z]{1}[0-9]{6}$|^WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵青藏川宁琼]{1}[0-9]{5}$|^使[0-9]{3}[A-Z]{1}$|^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵青藏川宁琼]{1}[A-Z]{1}挂[0-9]{4}$|^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵青藏川宁琼]{1}[A-Z]{1}警[0-9]{4}$|^粤Z[A-HJ-NP-Z0-9]{4,5}(港|澳)?$/[^78^]}")
    private String licensePlateRegex;
    
    @Autowired
    private VehicleService vehicleService;

    @Autowired
    private VehicleAccessRelaService vehicleAccessRelaService;
    
    @Autowired
    private BarrierGateService barrierGateService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "01.新增车辆和特种设备信息", notes = "01.新增车辆和特种设备信息")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> addVehicle(@Validated @RequestBody VehicleAddDTO vehicleAddDTO) throws GeneralSimpleException {
        log.info("01.新增车辆和特种设备信息：{}", vehicleAddDTO);
        String objId = vehicleService.addVehicle(vehicleAddDTO);
        boolean status = false;
        if(StringUtils.isNotBlank(objId)){
            status = true;
            CompletableFuture.runAsync(() -> barrierGateService.issuedCarByVehicleId(objId));
        }
        
        log.info("01.新增车辆和特种设备信息：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "02.删除车辆和特种设备信息", notes = "02.删除车辆和特种设备信息")
    @PostMapping(value = "/delByObjId", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> delByObjId(@Validated @RequestBody VehicleDelDTO vehicleDelDTO) throws GeneralSimpleException{
        log.info("02.删除车辆和特种设备信息：{}", vehicleDelDTO);
        boolean status = vehicleService.delVehicle(vehicleDelDTO);
        log.info("02.删除车辆和特种设备信息：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "03.分页查询车辆和特种设备信息", notes = "03.分页查询车辆和特种设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findVehicle", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<VehiclePageVO>> findVehicle(@RequestBody @Validated VehicleFindDTO vehicleFindDTO) throws GeneralSimpleException {
        log.info("03.分页查询车辆和特种设备信息：{}", vehicleFindDTO);
        IPage<VehiclePageVO> vehiclePageVOIPage = vehicleService.findVehicle(vehicleFindDTO);
        log.info("03.分页查询车辆和特种设备信息：success");
        return this.response(vehiclePageVOIPage);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "04.分页查询车辆和特种设备信息-黑名单", notes = "04.分页查询车辆和特种设备信息-黑名单")
    @PostMapping(value = "/findVehicleBlackList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<VehiclePageVO>> findVehicleBlackList(@RequestBody @Validated VehicleFindDTO vehicleFindDTO) throws GeneralSimpleException {
        log.info("04.分页查询车辆和特种设备信息-黑名单：{}", vehicleFindDTO);
        IPage<VehiclePageVO> vehiclePageVOIPage = vehicleService.findVehicleBlackList(vehicleFindDTO);
        log.info("04.分页查询车辆和特种设备信息-黑名单：success");
        return this.response(vehiclePageVOIPage);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "05.加入/取消黑名单(车辆和特种设备)", notes = "05.加入/取消黑名单(车辆和特种设备)")
    @PostMapping(value = "/addBlacklistVehicle", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> addBlacklistVehicle(@Validated @RequestBody VehicleBlackListDTO vehicleBlackListDTO) throws GeneralSimpleException {
        log.info("05.加入/取消黑名单：{}", vehicleBlackListDTO);
        Boolean status = vehicleService.addBlacklistVehicle(vehicleBlackListDTO);
        log.info("05.加入/取消黑名单：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "06.黑名单--解除黑名单",notes = "06.解除黑名单(车辆和特种设备)" )
    @PostMapping(value = "/liftBlacklistVehicle", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> liftBlacklistVehicle(@Validated @RequestBody VehicleIdDTO vehicleIdDTO) throws GeneralSimpleException {
        log.info("6.黑名单--解除黑名单：{}", vehicleIdDTO);
        Boolean status = vehicleService.liftBlacklistVehicle(vehicleIdDTO);
        log.info("6.黑名单--解除黑名单：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "7.查询车辆和特种设备信息-车牌号", notes = "07.查询人员信息-车牌号")
    @PostMapping(value = "/findByCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<VehicleLicensePlateVO> findByCar(@Validated @RequestBody VehicleLicensePlateDTO vehicleLicensePlateDTO) throws GeneralSimpleException {
        log.info("7.查询车辆和特种设备信息-车牌号：{}", vehicleLicensePlateDTO);
        VehicleLicensePlateVO vehicleLicensePlateVO = vehicleService.findByCar(vehicleLicensePlateDTO);
        log.info("7.查询车辆和特种设备信息-车牌号：success");
        return this.response(vehicleLicensePlateVO);
    }

    @ApiOperation(value = "08.根据ID查询车辆信息", notes = "08.根据ID查询车辆信息")
    @PostMapping(value = "/findById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<VehicleLicensePlateVO> findById(@Validated @RequestBody VehicleDelDTO vehicleIdDTO) throws GeneralSimpleException {
        log.info("08.根据ID查询人员信息：{}", vehicleIdDTO);
        VehicleLicensePlateVO vehicleIdVO = vehicleService.findById(vehicleIdDTO);
        log.info("08.根据ID查询人员信息：success");
        return this.response(vehicleIdVO);
    }

    @ApiOperation(value = "09.更新车辆设备信息")
    @PostMapping(value = "/updateVehicle", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateVehicle(@Validated @RequestBody VehicleUpdateDTO vehicleUpdateDTO) throws GeneralSimpleException {
        log.info("09.更新车辆设备信息：{}", vehicleUpdateDTO);
        boolean status = vehicleService.updateVehicle(vehicleUpdateDTO);
        log.info("09.更新车辆设备信息：success");
        return this.response(status);
    }

    @ApiOperation(value = "10.分页查询准入列表")
    @PostMapping(value = "/pageByAccess", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<VehicleAccessPageVO>> pageByAccess(@Validated @RequestBody VehicleAccessPageDTO vehicleAccessPageDTO) throws GeneralSimpleException {
        log.info("10.分页查询准入列表：{}", vehicleAccessPageDTO);
        IPage<VehicleAccessPageVO> regulationsRecordVOIPage = vehicleAccessRelaService.pageByAccess(vehicleAccessPageDTO);
        log.info("10.分页查询准入列表：success");
        return this.response(regulationsRecordVOIPage);
    }

    @ApiOperation(value = "11.删除准入工程")
    @PostMapping(value = "/delAccessWork", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> delAccessWork(@Validated @RequestBody VehicleAccessIdDTO vehicleAccessIdDTO) throws GeneralSimpleException {
        log.info("11.删除准入工程：{}", vehicleAccessIdDTO);
        Boolean status = vehicleAccessRelaService.delAccessWork(vehicleAccessIdDTO);
        log.info("11.删除准入工程：success");
        return this.response(status);
    }

    @ApiOperation(value = "12.修改准入工程的准入周期")
    @PostMapping(value = "/updateAccessCycle", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateAccessCycle(@Validated @RequestBody VehicleAccessCycleSaveDTO vehicleAccessCycleSaveDTO) throws GeneralSimpleException {
        log.info("12.修改准入工程的准入周期：{}", vehicleAccessCycleSaveDTO);
        Boolean status = vehicleAccessRelaService.updateAccessCycle(vehicleAccessCycleSaveDTO);
        log.info("12.修改准入工程的准入周期：success");
        return this.response(status);
    }
    
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "13.查询车牌号校验", notes = "13.查询车牌号校验")
    @GetMapping(value = "/licensePlateRegex", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<String> licensePlateRegex() {
        log.info("13.查询车牌号校验");
        return this.response(licensePlateRegex);
    }
}
